﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace Photo
{
    public partial class IMG : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                AlbumDO objab = new AlbumBL().Select(new AlbumDO() { AlbumID = Int32.Parse(Request.QueryString["AlbumID"].ToString()) });
                txtTenAlbum.Text = objab.Title;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
           
            initObject(fuImage1, txtTitle1.Text, txtMota1.Text);
            initObject(fuImage2, txtTitle2.Text, txtMota2.Text);
            initObject(fuImage3, txtTitle3.Text, txtMota3.Text);
            initObject(fuImage4, txtTitle4.Text, txtMota4.Text);
            initObject(fuImage5, txtTitle5.Text, txtMota5.Text);
            //initObject(fuImage6, txtTitle6.Text, txtMota6.Text);
            //initObject(fuImage7, txtTitle7.Text, txtMota7.Text);
            //initObject(fuImage8, txtTitle8.Text, txtMota8.Text);
            //initObject(fuImage9, txtTitle9.Text, txtMota9.Text);
            //initObject(fuImage10, txtTitle10.Text, txtMota10.Text);
            Functions.Alert("Cập nhật thành công!", "ListAlbum.aspx");
        }

        private void initObject(FileUpload fulImage, string title,string mota)
        {
            if (!string.IsNullOrEmpty(fulImage.FileName))
            {
                IMGDO objIMG = new IMGDO();
                objIMG.Title = title;
                objIMG.Mota = mota;

                objIMG.Url = UploadFile(fulImage, title);
                objIMG.AlbumID = Int32.Parse(Request.QueryString["AlbumID"].ToString());
                new IMGBL().Insert(objIMG);
            }
        }

        private string UploadFile(FileUpload fulImages, string title)
        {
            if (!string.IsNullOrEmpty(fulImages.FileName))
            {
                string FileName = string.Format("{0}{1}", Functions.Change_AV(title) + "-" + DateTime.Now.ToString("ddMMyyyyhhmmss"), fulImages.FileName.Substring(fulImages.FileName.LastIndexOf(".")));
                string SaveLocation = string.Format("{0}\\{1}", Server.MapPath("~/Media/"), FileName);
                fulImages.SaveAs(SaveLocation);
                return FileName;
            }
            return string.Empty;
        }

    }
}
